package dyyx.util;

import java.io.File;

import dyyx.dto.TimeInfo;

public class RunStatusUtil {

    public static final RunStatus runStatus   = new RunStatus();

    public static final String    DEFAULT_TAG = "0";

    static {
        buildWorkDirInfo();
    }

    public static void init() {

    }

    public static void putExtraData(String key, Object value) {
        if (key == null || value == null) {
            return;
        }
        runStatus.extraData.put(key, value);
    }

    public static void logInfo(String key, String info) {
        if (CommUtil.isBlank(key) || CommUtil.isBlank(info)) {
            return;
        }
        runStatus.infoMap.put(key, info);
    }

    public static void logTime(String key, long time) {
        if (key == null || time < 0) {
            return;
        }
        TimeInfo ti = runStatus.timeInfoMap.get(key);
        if (ti == null) {
            ti = new TimeInfo();
            runStatus.timeInfoMap.put(key, ti);
        }

        ti.setCurrent(time);

        if (time == 0) {
            ti.zeroCount.incrementAndGet();
            // return;
        }

        ti.count.incrementAndGet();
        ti.sum.addAndGet(time);
        if (time > ti.getMax()) {
            ti.setMax(time);
            ti.setMaxTime(System.currentTimeMillis());
        }
        // long avg = sum / count;
        // ti.avg = avg;
        if (time < 100) {
            return;
        }

        if (time >= 100) {
            ti.countGt100.getAndIncrement();
        }

        if (time >= 200) {
            ti.countGt200.getAndIncrement();
        }
        if (time >= 500) {
            ti.countGt500.getAndIncrement();
        }

        if (time >= 1000) {
            ti.countGt1000.getAndIncrement();
        }

    }

    public static void logError(Throwable e) {
        if (e == null) {
            return;
        }
        logError(DEFAULT_TAG, e);
    }

    public static void logError(String tag, Throwable e) {
        if (e == null) {
            return;
        }
        if (tag == null) {
            tag = DEFAULT_TAG;
        }
        String name = e.getClass().getSimpleName();
        if (CommUtil.isBlank(name)) {
            return;
        }
        runStatus.updateCount(tag + "#" + name);
        // runStatus.updateInfo(name, e+","+new Date());
        runStatus.logError(tag, e);

    }

    public static void updateStaticInfo(String key, String msg) {
        runStatus.updateStatisInfo(key, msg);
    }
    
    public static void updateCount(String key, long delta) {
        runStatus.updateCount(key, delta);
    }
    
    public static void updateCount(String key) {
        runStatus.updateCount(key, 1);
    }

   

    private static void buildWorkDirInfo() {
        try {

            String userDir = System.getProperty("user.dir");
            if (userDir == null) {
                userDir = "";
            }
            updateStaticInfo("workdir.userdir", userDir);

            File dir = new File("");

            updateStaticInfo("workdir.dir", dir.getAbsolutePath());

        } catch (Throwable e) {
            updateStaticInfo("buildWorkDirInfoError", e + "");
        }
    }

}
